package com.sjgs.gis.service;

import com.sjgs.gis.domain.ScheduleJob;
import com.sjgs.gis.domain.enumeration.JobOperateEnum;
import com.sjgs.gis.repository.ScheduleJobRepository;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

import java.util.List;
import java.util.Optional;


@Service
public class ScheduleJobService {

    @Autowired
    private QuartzService quartzService;

    private final ScheduleJobRepository scheduleJobRepository;

    public ScheduleJobService(ScheduleJobRepository scheduleJobRepository) {
        this.scheduleJobRepository = scheduleJobRepository;
    }

    List<ScheduleJob> findAll() {
        return scheduleJobRepository.findAll();
    }

    public void add(ScheduleJob job) {
        //此处省去数据验证
        scheduleJobRepository.save(job);
        //加入job
        try {
            quartzService.addJob(job);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void start(long id) {
        //此处省去数据验证
        Optional<ScheduleJob> scheduleJobOptional = scheduleJobRepository.findById(id);
        if (scheduleJobOptional.isPresent()) {
            ScheduleJob job = scheduleJobOptional.get();
            job.setStatus(1);
            scheduleJobRepository.save(job);
            //执行job
            try {
                quartzService.operateJob(JobOperateEnum.START, job);
            } catch (SchedulerException e) {
                e.printStackTrace();
            }
        }

    }

    public void pause(long id) {
        //此处省去数据验证
        Optional<ScheduleJob> scheduleJobOptional = scheduleJobRepository.findById(id);
        if (scheduleJobOptional.isPresent()) {
            ScheduleJob job = scheduleJobOptional.get();
            job.setStatus(2);
            scheduleJobRepository.save(job);
            //执行job
            try {
                quartzService.operateJob(JobOperateEnum.PAUSE, job);
            } catch (SchedulerException e) {
                e.printStackTrace();
            }
        }

    }

    public void delete(long id) {
        //此处省去数据验证
        Optional<ScheduleJob> scheduleJobOptional = scheduleJobRepository.findById(id);
        if (scheduleJobOptional.isPresent()) {
            ScheduleJob job = scheduleJobOptional.get();
            try {
                quartzService.operateJob(JobOperateEnum.DELETE, job);
                scheduleJobRepository.deleteById(id);
            } catch (SchedulerException e) {
                e.printStackTrace();
            }
        }
    }

    public void startAllJob() {
        //此处省去数据验证
        List<ScheduleJob> scheduleJobs = findAll();
        scheduleJobs.stream().forEach(job -> {
            job.setStatus(1);
            scheduleJobRepository.save(job);
        });
        //执行job
        try {
            quartzService.startAllJob();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    public void pauseAllJob() {
        List<ScheduleJob> scheduleJobs = findAll();
        scheduleJobs.stream().forEach(job -> {
            job.setStatus(2);
            scheduleJobRepository.save(job);
        });
        //执行job
        try {
            quartzService.pauseAllJob();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
}
